
package linuxbox.exception;

import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;

import linuxbox.util.*;
import linuxbox.BoxStatic;

public class ExceptionHelper {
    protected Map errorMsgMap = new HashMap();

	// Exception type definitions
	public static final int DOWNLOAD = 201;
	public static final int SYSTEM = 202;
	public static final int ADMIN = 203;
	public static final int ADDRBOOK = 204;
	public static final int BBS = 205;
	public static final int STATUS = 206;
	public static final int COMMON = 207;
        public static final int FILELIB = 208;

    public static final int RESOURCE = 102;
    public static final int CALENDAR = 103;
    public static final int TODO = 104;
    public static final int NEWS = 105;
    public static final int RESERVATION = 106;
    public static final int POLL = 107;

    public ExceptionHelper(String fn) throws Exception {
    	initMessageBundle(fn);
    }

    private void initMessageBundle(String fn) throws Exception {
        BoxStatic.logger.debug("messageProperties - " + fn);

        SAXBuilder builder = new SAXBuilder(false);
        Document doc = builder.build(new FileInputStream(fn));
        Element root = doc.getRootElement();
        for (Iterator it1 = root.getChildren("Module").iterator();
        it1.hasNext(); ) {
            Element module = (Element)it1.next();
            for (Iterator it2 = module.getChildren("ErrMsg").iterator();
            it2.hasNext(); ) {
                Element el = (Element)it2.next();
                String name = el.getAttributeValue("id");
                String value = el.getTextTrim();

                if (name == null || value == null) continue;

                errorMsgMap.put(name.trim(), value);
            }
        }
    }

    public String getErrorMessage(LinuxboxException e) {
		String retVal = null;

		String errorID = e.getErrorID();

        if (!WebTools.UNKNOWN_STRING.equals(errorID)) {
            retVal = (String)errorMsgMap.get(errorID);
        }

        return retVal;
	}
}
